//
//  UIViewController.m
//  JMT
//
//  Created by bianruifeng on 15/10/25.
//  Copyright © 2015年 bianruifeng. All rights reserved.
//

#import "UIViewController.h"
#import <objc/runtime.h>


@implementation UIViewController (base)

static void XY_swizzleInstanceMethod(Class c, SEL original, SEL replacement) {
    Method a = class_getInstanceMethod(c, original);
    Method b = class_getInstanceMethod(c, replacement);
    if (class_addMethod(c, original, method_getImplementation(b), method_getTypeEncoding(b)))
    {
        class_replaceMethod(c, replacement, method_getImplementation(a), method_getTypeEncoding(a));
    }
    else
    {
        method_exchangeImplementations(a, b);
    }
}
+ (void)load
{
    XY_swizzleInstanceMethod([UIViewController class], @selector(loadView), @selector(xy__loadView));
    XY_swizzleInstanceMethod([UIViewController class], @selector(viewDidLoad), @selector(xy__viewDidLoad));
    XY_swizzleInstanceMethod([UIViewController class], NSSelectorFromString(@"dealloc"), @selector(xy__dealloc));
    XY_swizzleInstanceMethod([UIViewController class], @selector(didReceiveMemoryWarning), @selector(xy__didReceiveMemoryWarning));
}

- (void)xy__loadView
{
    [self xy__loadView];
    
    if ([self respondsToSelector:@selector(createFields)])
        [self performSelector:@selector(createFields)];
    
    if ([self respondsToSelector:@selector(createViews)])
        [self performSelector:@selector(createViews)];
    
    if ([self respondsToSelector:@selector(enterBackground)])
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
    }
    
    if ([self respondsToSelector:@selector(enterForeground)])
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
    }
    
    if ([self respondsToSelector:@selector(createEvents)])
        [self performSelector:@selector(createEvents)];
}


- (void)xy__dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    if ([self respondsToSelector:@selector(destroyEvents)])
        [self performSelector:@selector(destroyEvents)];
    
    if ([self respondsToSelector:@selector(destroyViews)])
        [self performSelector:@selector(destroyViews)];
    
    if ([self respondsToSelector:@selector(destroyFields)])
        [self performSelector:@selector(destroyFields)];
    
    [self xy__dealloc];
}

- (void)xy__viewDidLoad
{
    if ([self respondsToSelector:@selector(loadData)])
        [self performSelector:@selector(loadData)];
    
    [self xy__viewDidLoad];
}

- (void)xy__didReceiveMemoryWarning
{
    if ([self isViewLoaded] && [self.view window] == nil)
    {
        if ([self respondsToSelector:@selector(cleanData)])
            [self performSelector:@selector(cleanData)];
    }
    
    [self xy__didReceiveMemoryWarning];
}


#pragma mark - api
// 对外的接口

#pragma mark - rewrite
// 额外的重写的父类的方法

#pragma mark - private

#pragma mark - 响应 model 的地方
#pragma mark 1 notification
#pragma mark 2 KVO

#pragma mark - 响应 view 的地方
#pragma mark 1 target-action
#pragma mark 2 delegate dataSource protocol

#pragma mark -

@end